Micro Chat

Introduction

Two micro:bit connected via radio

Use the radio to send and receive messages with another micro:bit.

Handle buttons

Add an event to run code when ||input:button A is pressed||.

input.onButtonPressed(Button.A, function() {
})

Sending a message

Add code to ||radio:send a string|| over ||radio:radio|| when ||input:button A is pressed||. Every micro:bit nearby will receive this message.

input.onButtonPressed(Button.A, function() {
    // @highlight
    radio.sendString(":)")
})

Receiving a message

Put in another event to run code when a ||radio:string is received|| over ||radio:radio||.

radio.onReceivedString(function (receivedString) {
})

Displaying text

Inside the event, add code to ||basic:show|| the ||variables:receivedString||.

radio.onReceivedString(function (receivedString) {
    // @highlight
    basic.showString(receivedString)
})

Testing in the simulator

Press button A on the simulator, you will notice that a second micro:bit appears (if your screen is too small, the simulator might decide not to show it). Try pressing A again and notice that the “:)” message gets displayed on the other micro:bit.

Try it for real

If you two micro:bits, download the program to each one. Press button A on one and see if the other gets a message.

Groups

Add code to ||radio:set the group|| number of your program. You will only receive messages from micro:bits within the same group. Use this to avoid receiving messages from every micro:bit that is transmitting.

radio.setGroup(123)
radio